/*
 *   AS IS License
 * ==Naive Coders==
 */
package org.naive.metawatch.converter.parser;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.text.ParseException;

/**
 *
 * @author gbu.000
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ParsedFixedIntArray {

    int length();

    FieldSize itemSize();

    public class FieldParser {

        public static Object parse(ParsedFixedIntArray pia, ParseContext px, Field f)
                throws Exception {
            Class<?> componentType = f.getType().getComponentType();
            int size = pia.length();
            return parse(px, size, pia.itemSize(), componentType);
        }

        public static Object parse(ParseContext px,
                int length, FieldSize itemSize, Class<?> componentType)
                 throws ParseException {
            Object a = Array.newInstance(componentType, length);
            for (int i = 0; i < length; i++) {
                Array.set(a, i, px.read(itemSize));
            }
            return a;
        }
    }
}
